#ifndef GRID_H
#define GRID_H

#include <vector>
#include <cstdio>
#include "tile.h"
#include "player.h"

class Object;
class Package;
class Objective;

class Grid
{
public:
    std::vector<std::vector<Tile*> > tiles;
    Tile* start;
    Tile* stop;
    int sceneWidth;
    int sceneHeight;
    Player* player;
    std::map<ot::ObjectType,std::vector<Object*> > objects;
    Grid()
    {
    }

    void printState()const
    {

        for(int i=0;i<height();++i)
        {
            for(int j=0;j<width();++j)
                printf("%d ",tiles[i][j]->state);
            printf("\n");
        }
        printf("\n\n");

    }

    Grid* copy()
    {

        Grid* g=new Grid;
        int sizeH=sceneHeight/Tile::TILE_SIZE;
        int sizeW=sceneWidth/Tile::TILE_SIZE;
        for (int i = 0; i < sizeH; ++i)
        {
            g->tiles.push_back(std::vector<Tile*>(sizeW));
            //g->tiles[i].resize(sizeW);
            for(int j=0;j<sizeW;++j)
                g->tiles[i][j]=new Tile(tiles[i][j]);
        }
        g->objects=objects;
        g->player=player;
        g->start=start;
        g->stop=stop;
        g->sceneHeight=sceneHeight;
        g->sceneWidth=sceneWidth;

        return g;

    }
    void build(int width, int height)
    {
        sceneWidth=width;
        sceneHeight=height;
        int sizeH=height/Tile::TILE_SIZE;
        int sizeW=width/Tile::TILE_SIZE;
        tiles.clear();


        tiles.resize(sizeH);
        for (int i = 0; i < sizeH; ++i)
        {
            tiles[i].resize(sizeW);
            for(int j=0;j<sizeW;++j)
                tiles[i][j]=new Tile(j*Tile::TILE_SIZE,i*Tile::TILE_SIZE);
        }
    }

    void refresh()
    {
        for(auto &row:tiles)
            for(auto &tile:row)
                if (tile->state==OCCUPIED)tile->state=EMPTY;
    }

    int width() const
    {
        return tiles.front().size();
    }

    int height() const
    {
        return tiles.size();
    }


};

#endif // GRID_H
